<template>
  <div>
    <el-row :gutter="10">
      <el-col :span="4">
        <el-card>
          <div class="title">
            <div class="round mr">
              <el-icon>
                <Document />
              </el-icon>
            </div>
            <h4>今日总收入(元)</h4>
          </div>
          <div class="total mt">
            <h1>{{ formatNumberToThousands(11234.1) }}</h1>
            <div class="percent" style="color: green">-21%</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="4">
        <el-card>
          <div class="title">
            <div class="round mr">
              <el-icon>
                <Document />
              </el-icon>
            </div>
            <h4>今日总收入(元)</h4>
          </div>
          <div class="total mt">
            <h1>{{ formatNumberToThousands(1234.5) }}</h1>
            <div class="percent" style="color: green">-21%</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="4">
        <el-card>
          <div class="title">
            <div class="round mr">
              <el-icon>
                <Document />
              </el-icon>
            </div>
            <h4>今日总收入(元)</h4>
          </div>
          <div class="total mt">
            <h1>{{ formatNumberToThousands(239824) }}</h1>
            <div class="percent" style="color: green">-21%</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="4">
        <el-card>
          <div class="title">
            <div class="round mr">
              <el-icon>
                <Document />
              </el-icon>
            </div>
            <h4>今日总收入(元)</h4>
          </div>
          <div class="total mt">
            <h1>{{ formatNumberToThousands(1111.2) }}</h1>
            <div class="percent" style="color: green">-21%</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="4">
        <el-card>
          <div class="title">
            <div class="round mr">
              <el-icon>
                <Document />
              </el-icon>
            </div>
            <h4>今日总收入(元)</h4>
          </div>
          <div class="total mt">
            <h1>{{ formatNumberToThousands(1234556.25) }}</h1>
            <div class="percent" style="color: green">-21%</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="4">
        <el-card>
          <div class="title">
            <div class="round mr">
              <el-icon>
                <Document />
              </el-icon>
            </div>
            <h4>今日总收入(元)</h4>
          </div>
          <div class="total mt">
            <h1>239824</h1>
            <div class="percent" style="color: green">-21%</div>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <el-card class="mt">
      <div ref="chartRef" style="width: 100%; height: 300px"></div>
    </el-card>
    <el-card class="mt">
      <el-input v-model.trim="name" style="max-width: 400px;" place-holder="请输入站点名称">
        <template #append>
          <el-button icon="Search" @click="getData"></el-button>
        </template>
      </el-input>
      <el-table :data="tableData" v-loading="loading">
        <el-table-column type="index" label="序号" width="50"></el-table-column>
        <el-table-column label="充电站名称" prop="name"></el-table-column>
        <el-table-column label="充电站ID" prop="id"></el-table-column>
        <el-table-column label="所属城市" prop="city"></el-table-column>
        <el-table-column label="充电桩总量(个)" prop="count"></el-table-column>
        <el-table-column label="单日总收入(元)" prop="day" width="150" sortable>
          <template #default="scope">
            <span>{{ scope.row.day }}</span>
            <el-tag :type="scope.row.percent > 0 ? 'danger' : 'success'" class='ml'>
              {{  scope.row.percent > 0 ? `+${scope.row.percent}%` : `${scope.row.percent}%` }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="月度总收入(万元)" prop="month" width="150">
          <template #default="scope">
            <span>{{ scope.row.month }}</span>
            <el-tag :type="scope.row.mpercent > 0 ? 'danger' : 'success'" class='ml'>
              {{  scope.row.mpercent > 0 ? `+${scope.row.mpercent}%` : `${scope.row.mpercent}%` }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="电费营收(元)"
          prop="electricity"
        ></el-table-column>
        <el-table-column
          label="停车费营收(元)"
          prop="parkingFee"
        ></el-table-column>
        <el-table-column
          label="服务费营收(元)"
          prop="serviceFee"
        ></el-table-column>
        <el-table-column label="会员储值金(元)" prop="member"></el-table-column>
      </el-table>
      <el-pagination
      class="mt mb fr"
      background
      v-model:current-page="pageInfo.page"
      v-model:page-size="pageInfo.pageSize"
      :page-sizes="[10, 20, 30, 40]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="totals"
      @size-change="handleSizechange"
      @current-change="handlePageNochange"
    />
    </el-card>
  </div>
</template>

<script setup lang="ts">
import formatNumberToThousands from '@/utils/toThousands';
import { onMounted, reactive, ref } from 'vue';
import { chartApi, revenueApi } from '@/api/chargingStation';
import { useChart } from '@/hooks/useChart';
import { usePagination } from '@/hooks/usePagination';
const chartRef = ref(null);
const tableData = ref([]);
const loading = ref(false);
const name = ref('')
const setChartData = async () => {
  const chartOptions = reactive({
    tooltip: {
      trigger: 'axis',
    },
    legend: {
      data: [],
    },
    xAxis: {
      type: 'category',
      data: ['一月', '二月', '三月', '四月', '五月', '六月', '七月'],
    },
    yAxis: [
      {
        type: 'value',
        name: '销售',
        position: 'left',
      },
      {
        type: 'value',
        name: '访问量',
        position: 'right',
      },
    ],
    series: [
      {
        name: '',
        type: 'bar',
        data: [],
        yAxisIndex: 0,
        itemStyle: {
          color: '#409eff',
        },
      },
      {
        name: '',
        type: 'line',
        data: [],
        yAxisIndex: 1,
        itemStyle: {
          color: '#409eff',
        },
        smooth: true,
      },
    ],
  });
  const res = await chartApi();
  chartOptions.legend.data = res.data.list.map((item: any) => item.name);
  for (let i = 0; i < res.data.list.length; i++) {
    chartOptions.series[i].data = res.data.list[i].data;
    chartOptions.series[i].name = res.data.list[i].name;
  }
  return chartOptions;
};

useChart(chartRef, setChartData);

const getData = async () => {
  loading.value = true;
  const res = await revenueApi({
   ...pageInfo,
    name: name.value,
  });
  loading.value = false;
  tableData.value = res.data?.list.map((item:any)=>{
    return {
      ...item,
      day:item.electricity + item.parkingFee + item.serviceFee +item.member
    }
  })
  setTotals(res.data.total)
};

const {totals,pageInfo,handleSizechange,handlePageNochange,setTotals} = usePagination(getData)
onMounted(() => {
  getData();
});
</script>

<style lang="less" scoped>
.title {
  display: flex;
  align-items: center;
  .round {
    width: 30px;
    height: 30px;
    border-radius: 50%;
    background-color: rgb(235, 236, 245);
    text-align: center;
    line-height: 30px;
  }
  h4 {
    color: #666;
  }
}
.total {
  display: flex;
  align-items: center;
  h1 {
    font-size: 20px;
    margin-right: 10px;
  }
  .percent {
    display: inline-block;
    padding: 3px 5px;
    height: 20px;
    font-size: 12px;
    background-color: rgb(235, 247, 239);
    border-radius: 2px;
    line-height: 20px;
  }
}
</style>
